
package combat.dudes.server.protocol.implementation;

import combat.dudes.DudesConfig;
import combat.dudes.protocol.data.DoublePoint;
import combat.dudes.protocol.interfaces.OnboardSystemInt;
import combat.dudes.protocol.interfaces.OnboardWeaponSystemInt;
import combat.dudes.protocol.interfaces.TeamInt;
import combat.dudes.protocol.interfaces.VehicleInt;
import combat.dudes.protocol.types.OnboardSystemStatus;
import combat.dudes.protocol.types.OnboardSystemType;
import combat.dudes.protocol.types.ProjectileType;
import combat.dudes.protocol.types.WeaponSystemType;


/**
 * @author Laurens Andriessen
 * @author Jesper Keijzer
 * @version 1.0
 */
public class OnboardWeaponSystem extends OnboardSystem implements OnboardWeaponSystemInt {

	private static final long serialVersionUID = 1L;
	private int ammo = 0;
	private WeaponSystemType weaponSystemType = WeaponSystemType.UNKNOWN;

	public OnboardWeaponSystem() {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.WEAPON);
	}

	public OnboardWeaponSystem(int ammo, WeaponSystemType weaponSystemType) {
		super(OnboardSystemStatus.ONLINE, OnboardSystemType.WEAPON);
		setAmmo(ammo);
		setWeaponSystemType(weaponSystemType);
	}


	@Override
	public int getAmmo() {
		return ammo;
	}

	@Override
	public void setAmmo(int ammo) {
		this.ammo = ammo;
	}

	@Override
	public void fire(Object target) {
		if (getAmmo() > 0 && getOnboardSystemStatus().equals(OnboardSystemStatus.ONLINE)) {
			DoublePoint weaponPosition = null;
			TeamInt weaponTeam = null;
			// Get sensor(vehicle) position
			for (TeamInt team : Simulation.instance().getTeams())
				for (VehicleInt vehicle : team.getVehicles())
					for (OnboardSystemInt onboardSystem : vehicle.getOnboardSystems())
						if (this.equals(onboardSystem)) {
							weaponTeam = team;
							weaponPosition = vehicle.getPosition();
						}
			if (weaponTeam != null && weaponPosition != null) {
				Projectile newProjectile = null;
				switch (this.getWeaponSystemType()) {
					case UNKNOWN:
						newProjectile = new Projectile(weaponPosition, target, DudesConfig.SharedVars.getMaxDistance(ProjectileType.UNKNOWN), ProjectileType.UNKNOWN);
						break;
					case UNGUIDED_MISSILELAUNCHER:
						newProjectile = new Projectile(weaponPosition, target, DudesConfig.SharedVars.getMaxDistance(ProjectileType.MISSILE_UNGUIDED), ProjectileType.MISSILE_UNGUIDED);
						break;
					case GUIDED_MISSILELAUNCHER:
						newProjectile = new Projectile(weaponPosition, target, DudesConfig.SharedVars.getMaxDistance(ProjectileType.MISSILE_GUIDED), ProjectileType.MISSILE_GUIDED);
						break;
					case TORPEDOLAUNCHER:
						newProjectile = new Projectile(weaponPosition, target, DudesConfig.SharedVars.getMaxDistance(ProjectileType.TORPEDO), ProjectileType.TORPEDO);
						break;
					case GOALKEEPER:
						// TODO action of goalkepper
						break;
					default:
						newProjectile = new Projectile(weaponPosition, target, DudesConfig.SharedVars.getMaxDistance(ProjectileType.UNKNOWN), ProjectileType.UNKNOWN);
						break;
				}
				if (newProjectile != null) {
					ammo--;
					weaponTeam.addProjectile(newProjectile);
				}
			}
		}
	}
	@Override
	public WeaponSystemType getWeaponSystemType() {
		return weaponSystemType;
	}

	@Override
	public void setWeaponSystemType(WeaponSystemType weaponSystemType) {
		this.weaponSystemType = weaponSystemType;
	}

}
